<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Friend extends My_Controller{
	
	public static  $m_Type = array(
			DOUBAN_TYPE,
	);
	
	public static $fMax = 200;
	
	public $uid = null;
	
	public static $m_baseID = 1000000;

	public function __construct()
	{
		parent::__construct();
		
		if (empty($this->m_arrUser))
		{
			redirect('account/login?reUrl=index.php/douban/email');
			exit;
		}
	
		$this->load->library('my_api',self::$m_Type);
	
		$this->load->model('msg_m');
	
	}
		
	public function index()
	{
		$this->m_arrSub = array(
				'common/header'  =>  'index',
				'common/footer'  => 'index',
		);
		$this->getSub();
		
		
		// 获取豆瓣的关注者且为朋友信息..
		$data = array(
				'id'        => $this->my_api->douban_uid,
				'tag'     => 1,
				'start'   => 0,
				'count'  => 100,
		);
		
		$doubanFollowList  = $this->my_api->douban->api('Miniblog.following.GET',$data)->makeRequest();
		
		$mass_list = array();
		
		foreach ($doubanFollowList as $follow)
		{
			if ($follow['id'] == 40273256)
			{
				$mass_list[] = array('id' =>$follow['id'],);
			}	
		}
		
		foreach ($mass_list as $mass)
		{
			$data = array(
					'title' => '这是一封测试豆邮..',
					'content' =>  "<a href='http://www.douban.com'>如有打扰请不要见怪</a>",
					'receiver_id' => $mass['id'],
					'captcha_token' =>1,
					'captcha_string' => 1,
					);
			
			$ret  = $this->my_api->douban->api('Doumail.mail.POST')->makeRequest($data);
			
			// TODO: 间隔一段时间发送 ..
			//Array ( [captcha_token] => sb70aBVfUNQ6Yb8fnudYAjsT [captcha_url] => http://www.douban.com/misc/captcha?id=sb70aBVfUNQ6Yb8fnudYAjsT&size=s )
			
			if (!empty($ret))
			{
				for ($i = 0;$i<5;$i++)
				{
					// TODO: 只能破解验证码
					$data = array(
							'title' => '这是一封测试豆邮..',
							'content' =>  "<a href='http://www.douban.com'>如有打扰请不要见怪</a>",
							'receiver_id' => $mass['id'],
							'captcha_token' =>$ret['captcha_token'],
							'captcha_string' => 1,
					);
						
					$ret  = $this->my_api->douban->api('Doumail.mail.POST')->makeRequest($data);
				}
				
			}
		}
	
		//$this->load->view('douban/rank',$this->data);
	}
	
	public function following()
	{
		$max = self::$fMax;
		$start = 0;

		$data = array(
				'id'        => $this->my_api->douban_uid,
				'start'   => $start,
				'count'  => $max,
		);
		
		$followingList  = $this->my_api->douban->api('Miniblog.following.GET',$data)->makeRequest();
		
		while (count($followingList) > $max)
		{
			$data = array(
					'id'        => $this->my_api->douban_uid,
					'start'   => $start + $max,
					'count'  => $max,
			);
			
			$addList  = $this->my_api->douban->api('Miniblog.following.GET',$data)->makeRequest();
			
			$followingList =  addArray($followingList,$addList);
		}
		
		
		$this->data['toList'] = array();
		
		foreach ($followingList as $v)
		{
			if (isset($v['utags']))
			{
				// 添加到全部 ..
				if (!isset($this->data['toList'][0]))
				{
						$this->data['toList'][0] = array(
						'info' => array('id' => 0,'name' => '全部','total' => 0),
						);
				}
				
				$this->data['toList'][0]['list'][] = array(
						'uid' =>$v['id'],
						'name' => $v['screen_name'],
						'url' => $v['url'],
						'avatar' => $v['small_avatar'],
						);
				
				$this->data['toList'][0]['info']['total'] += 1;
				// 添加到其他分类
				foreach ($v['utags'] as $tags)
				{
					$tagID       = $tags['id'];
					$tagName = $tags['name'];
					if (!isset($this->data['toList'][$tagID]))
					{
						$this->data['toList'][$tagID]['info'] = array('id' => $tagID,'name' => $tagName,'total' => 0);
					}
					
					$this->data['toList'][$tagID]['list'][] = array(
							'uid' =>$v['id'],
							'name' => $v['screen_name'],
							'url' => $v['url'],
							'avatar' => $v['small_avatar'],
					);
					
					$this->data['toList'][$tagID]['info']['total'] += 1;
				}
			
			}
		}
		
		$this->load->view('douban/email/following',$this->data);
	}
	
	public function followers()
	{
		$max = self::$fMax;
		$start = 0;
		
		$data = array(
				'id'        => $this->my_api->douban_uid,
				'start'   => $start,
				'count'  => $max,
		);
		
		$followersList  = $this->my_api->douban->api('Miniblog.followers.GET',$data)->makeRequest();
		
		while (count($followersList) > $max)
		{
			$data = array(
					'id'        => $this->my_api->douban_uid,
					'start'   => $start + $max,
					'count'  => $max,
			);
				
			$addList  = $this->my_api->douban->api('Miniblog.followers.GET',$data)->makeRequest();
				
			$followersList =  addArray($followersList,$addList);
		}
		
		$this->data['toList'] = array();
		
		foreach ($followersList as $v)
		{
			$this->data['toList'][] = array(
					'uid' =>$v['id'],
					'name' => $v['screen_name'],
					'url' => $v['url'],
					'avatar' => $v['small_avatar'],
			);
		}
		
		$this->data['total'] = count($followersList);
		
		$this->load->view('douban/email/followers',$this->data);
		
	}
	
	public function each()
	{
		
		$max = self::$fMax;
		$start = 0;
		
		$data = array(
				'id'        => $this->my_api->douban_uid,
				'start'   => $start,
				'count'  => $max,
		);
		
		$eachList  = $this->my_api->douban->api('Miniblog.suggestions.GET',$data)->makeRequest();
		
		while (count($eachList) > $max)
		{
			$data = array(
					'id'        => $this->my_api->douban_uid,
					'start'   => $start + $max,
					'count'  => $max,
			);
		
			$addList  = $this->my_api->douban->api('Miniblog.suggestions.GET',$data)->makeRequest();
		
			$eachList =  addArray($eachList,$addList);
		}
		
		$this->data['toList'] = array();
		
		foreach ($eachList as $v)
		{
			$this->data['toList'][] = array(
					'uid' =>$v['uid'],
					'name' => $v['screen_name'],
					'url' => $v['url'],
					'avatar' => $v['small_avatar'],
			);
		}
		
		$this->data['total'] = count($eachList);
		
		$this->load->view('douban/email/each',$this->data);
		
	}
	
}